//
//  EpointWebInfoDetailViewController_V2.m
//  EpointFrame5
//
//  Created by 李亚春 on 14-4-3.
//  Copyright (c) 2014年 Epoint. All rights reserved.
//

#import "EpointWebInfoDetailViewController_V2.h"
#import "EpointWebInfoSingleRecordModel.h"
#import "EpointWebInfoFeedBackModel.h"
#import "EpointInputViewController.h"
#import "EpointAttachDownloadListViewController.h"
#import "EpointWebInfoDetailAttachModel.h"

@interface EpointWebInfoDetailViewController_V2 ()
{
    UIWebView *wv;
    NSString *infoGuid;
    EpointWebInfoSingleRecordModel *infoModel;
    NSString *disContent;
    NSMutableArray *feedBackArray;
    PGBarItem *attachItem;
}
@end

@implementation EpointWebInfoDetailViewController_V2

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    infoGuid = [self.passParam objectForKey:@"InfoGuid"];
    
//    [self setRightBarButtonItemWithTitle:@"反馈" action:@selector(addFK)];
    
    CGRect rect = CGRectMake(0, 0, self.getFullContentFrame.size.width, self.getFullContentFrame.size.height-BottomBarHeight);
    wv = [[UIWebView alloc]initWithFrame:rect];
    wv.backgroundColor = [UIColor whiteColor];
    wv.opaque = NO;
    //wv.scalesPageToFit = YES;
    
    [self.view addSubview:wv];
    [self addPGTabBar];
    NSString *tmpDisContent = (NSString *)[DBFrameService getUserDefaults:infoGuid];
    [wv loadHTMLString:tmpDisContent baseURL:nil];
    
    [self getData];
}

- (void)addPGTabBar {
    PGTabBar *tabBar = [[PGTabBar alloc] initWithFrame:CGRectMake(0, AppHeight-SystemTakeHeight-BottomBarHeight, AppWidth, BottomBarHeight)];
    tabBar.delegate = self;
    attachItem = [[PGBarItem alloc] initWithTitle:@"附件" image:[UIImage imageNamed:@"mail_attach"] tag:0];
    PGBarItem *operationItem = [[PGBarItem alloc] initWithTitle:@"反馈" image:[UIImage imageNamed:@"operationMail"] tag:1];
    NSArray *items = @[attachItem, operationItem];
    [tabBar setBarItems:items];
    [self.view addSubview:tabBar];
}

-(void)pgTabBar:(PGTabBar *)tabBar didClickedAtIndex:(NSInteger)index
{
    switch (index) {
        case 0:
            [self downAttach];
            break;
        default:
            [self addFK];
            break;
    }
}

-(void)getData
{
    [self showMBPHUD];
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:infoGuid forKey:@"InfoGuid"];
    [params setObject:[self.passParam objectForKey:@"CategoryGuid"] forKey:@"CategoryGuid"];
    [self startTask:@"WebInfo_GetDetail_Task" withParams:params withTaskId:1];
}

-(void)getFKData
{
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:infoGuid forKey:@"InfoGuid"];
    [params setObject:[self.passParam objectForKey:@"CategoryGuid"] forKey:@"CategoryGuid"];
    [self startTask:@"EpointGetFeedBackListTask" withParams:params withTaskId:2];
}

-(void)addFK
{
    [EpointInputViewController involeInputViewController:self vctitle:@"反馈" preContent:@"" handler:^(NSString *text){
        //add by wangzhigang at 2014-12-23 09:20
        //desc 反馈内容为空验证
        if ([@"" isEqualToString:[text stringByReplacingOccurrencesOfString:@" " withString:@""]]) {
            [self toastMessage:@"反馈内容不能为空"];
            return ;
        }
        NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
        [params setObject:infoGuid forKey:@"InfoGuid"];
        [params setObject:text forKey:@"FeedBackContent"];
        [self startTask:@"WebInfo_AddFeedBack" withParams:params withTaskId:3];
        
    }];
}

-(void)refreshUIWithThreadGuid:(int)taskId andData:(NSObject *)obj
{
    [self hideMBPHUD];
    //处理异常信息
    NSMutableArray *arr = (NSMutableArray*)obj;
    if (![[arr objectAtIndex:0]isEqualToString:@"1"]) {
        [self toastMessage:[arr objectAtIndex:1]];
        return;
    }
    if (taskId==1) {
        NSMutableArray *listData = [arr objectAtIndex:2];
        infoModel = [listData objectAtIndex:0];
        if ([infoModel.infoProperty isEqualToString:@"Link"]) {
            NSLog(@"infoModel.LinkUrl=%@",infoModel.LinkUrl);
            [self performSelector:@selector(openLinkUrlWithSafari) withObject:self afterDelay:0.5];
            return;
        }
        disContent = [NSString stringWithFormat:@"%@<div id='oschina_title'>%@</div><div id='oschina_outline'>%@ %@</div><hr class=\"hr1\"/>%@",HTMLSTYPE1,infoModel.title,infoModel.postUserName,[[infoModel.infoDate componentsSeparatedByString:@" "] objectAtIndex:0],infoModel.content];
        [wv loadHTMLString:disContent baseURL:nil];
        [self getFKData];
        
        if (infoModel.AttachFiles.count>0) {
            /*
            UIButton *btAttach = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 44, 30)];
            [btAttach addTarget:self action:@selector(downAttach) forControlEvents:UIControlEventTouchUpInside];
            [btAttach setImage:[UIImage imageNamed:@"attachBtn_gray"] forState:UIControlStateNormal];
            [btAttach setFrame:CGRectMake(self.view.frame.size.width-btAttach.frame.size.width-10, self.view.frame.size.height-btAttach.frame.size.height-10, btAttach.frame.size.width, btAttach.frame.size.height)];
            UILabel *count = [[UILabel alloc]initWithFrame:CGRectMake(20, 13, 22, 15)];
            count.text = [NSString stringWithFormat:@"%i",infoModel.AttachFiles.count];
            count.textAlignment = NSTextAlignmentCenter;
            count.textColor = [UIColor blueColor];
            count.backgroundColor = [UIColor clearColor];
            [count setFont:[UIFont systemFontOfSize:10]];
            [btAttach addSubview:count];
            [self.view addSubview:btAttach];
             */
            attachItem.badgeValue = [NSString stringWithFormat:@"%i",infoModel.AttachFiles.count];
        }
    }
    else if(taskId==2)
    {
        feedBackArray = [arr objectAtIndex:2];
        NSString *fkStr = [NSString stringWithFormat:@"%@%@",disContent,[self createHTMLTableWithArray:feedBackArray]];
        [wv loadHTMLString:fkStr baseURL:nil];
        [DBFrameService setUserDefaults:fkStr key:infoGuid];
    }
    else if(taskId==3)
    {
        [self getFKData];
    }
}

-(void)openLinkUrlWithSafari{
  [self popBack];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:infoModel.LinkUrl]];
}

-(void)downAttach
{
    NSMutableArray *fileList = [[NSMutableArray alloc]init];
    
    for (EpointWebInfoDetailAttachModel *m in infoModel.AttachFiles) {
        NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
        [dic setObject:m.AttFileName forKey:@"FileName"];
        [dic setObject:m.AttachGuid forKey:@"FolderGuid"];
        m.DownLoadUrl = [EpointBizTools getRealDownloadUrl:m.DownLoadUrl];
        [dic setObject:m.DownLoadUrl forKey:@"FileUrl"];
        [dic setObject:m.isBigFile forKey:@"isBigFile"];
        [fileList addObject:dic];
    }
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:fileList forKey:@"FileList"];
    [params setObject:infoGuid forKey:@"FolderGuid"];
    EpointAttachDownloadListViewController *v = [[EpointAttachDownloadListViewController alloc]init];
    v.passParam = params;
    [self.navigationController pushViewController:v animated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

- (NSString *)createHTMLTableWithArray:(NSArray *)array {
    
    NSMutableString *feedHTML = [[NSMutableString alloc]initWithString:@""];
    if (array.count>0) {
        [feedHTML appendString:@"<br/><br/><TABLE style=\"BORDER-LEFT-WIDTH: 0px; BORDER-COLLAPSE: collapse; BORDER-RIGHT-WIDTH: 0px;\" cellSpacing=0 cellPadding=0 align=center border=0 width='100%'>"];
        [feedHTML appendString:@"<tr>"];
        [feedHTML appendString:@"<TD style=\"BORDER-RIGHT: medium none; BORDER-LEFT: medium none; BORDER-BOTTOM: medium none;background-color: #33A1C9;text-align: center;\" height=40>"];
        [feedHTML appendString:[NSString stringWithFormat:@"<font color=white><strong>信 息 反 馈</strong></font>"]];
        [feedHTML appendString:@"</td>"];
        [feedHTML appendString:@"</tr>"];
        for (int i=0; i<array.count; i++) {
            EpointWebInfoFeedBackModel *m = [array objectAtIndex:i];
            if (i%2 !=0) {
                [feedHTML appendString:@"<tr>"];
                [feedHTML appendString:@"<TD style=\"BORDER-RIGHT: medium none; BORDER-LEFT: medium none; BORDER-BOTTOM: medium none;background-color: #DCDCDC;\" height=40>"];
                [feedHTML appendString:[NSString stringWithFormat:@"<font color='#3D59AB'>%@&nbsp;&nbsp;%@</font>",m.DispName,m.BackTime]];
                [feedHTML appendString:@"</td>"];
                [feedHTML appendString:@"</tr>"];
                [feedHTML appendString:@"<tr>"];
                [feedHTML appendString:@"<td style=\"BORDER-RIGHT: medium none; BORDER-LEFT: medium none; BORDER-TOP: medium none;background-color: #DCDCDC;\" height=40><font color='#0B1746'>"];
                [feedHTML appendString:m.FeedBackContent];
                [feedHTML appendString:@"</font></td>"];
                [feedHTML appendString:@"</tr>"];
            }else{
                [feedHTML appendString:@"<tr>"];
                [feedHTML appendString:@"<TD style=\"BORDER-RIGHT: medium none; BORDER-LEFT: medium none; BORDER-BOTTOM: medium none;background-color: #FFFAF0;\" height=40>"];
                [feedHTML appendString:[NSString stringWithFormat:@"<font color='#5E2612'>%@&nbsp;&nbsp;%@</font>",m.DispName,m.BackTime]];
                [feedHTML appendString:@"</td>"];
                [feedHTML appendString:@"</tr>"];
                [feedHTML appendString:@"<tr>"];
                [feedHTML appendString:@"<td style=\"BORDER-RIGHT: medium none; BORDER-LEFT: medium none; BORDER-TOP: medium none;background-color: #FFFAF0;\" height=40><font color='#734A12'>"];
                [feedHTML appendString:m.FeedBackContent];
                [feedHTML appendString:@"</font></td>"];
                [feedHTML appendString:@"</tr>"];
            }
            
        }
        [feedHTML appendString:@"</table>"];
    }
    return feedHTML;
}

@end
